package com.qfedu.sign.common.third;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Map;

public class JedisUtil {
    private static final String RHOST="39.105.189.141";
    private static final int RPORT=6380;
    private static final String RPASS="qfjava";
    private static final int RTIMEOUT=3000;
    private static JedisPool pool;
    private Jedis jedis;
    static {
        //1实例化连接池配置对象
        JedisPoolConfig config=new JedisPoolConfig();
        config.setMaxWaitMillis(RTIMEOUT);
        config.setMaxTotal(1000);
        //2.实例化连接池
        pool=new JedisPool(config,RHOST,RPORT,RTIMEOUT);
    }
    public JedisUtil(){
        jedis=pool.getResource();
        jedis.auth(RPASS);
    }

    //新增
    public void addStr(String key,String val){
        jedis.set(key, val);
    }
    public void addStr(String key,String val,long seconds){
        jedis.setex(key,seconds, val);
    }
    public void addHash(String key,String f,String v){
        jedis.hset(key, f, v);
    }
    public void incr(String key){
        jedis.incr(key);//自增1
    }

    //查询
    public String getStr(String key){
        return jedis.get(key);
    }
    public boolean checkField(String key,String f){
        return jedis.hexists(key, f);
    }
    public Map<String,String> getHash(String key){
        return jedis.hgetAll(key);
    }
    //删除
    public void delHash(String key,String f){
        jedis.hdel(key,f);
    }
    //系统
    public long ttl(String key){
        return jedis.ttl(key);
    }
    public void expire(String key,long seconds){
        jedis.expire(key,seconds);
    }
    public void delKeys(String... keys){
        jedis.del(keys);
    }
    public boolean exists(String... keys)
    {
        return jedis.exists(keys)>0;
    }
    //释放连接对象
    public void distory(){
        pool.returnResource(jedis);
    }
}
